<?php

namespace app\common\model;


use think\facade\Db;
use think\Model;

 class CommonModel extends Model
{
//     protected  $name = '';
    public function itemsByIds($ids)
    {
        $return = [];
        if (empty($ids)) return $return;
        $data = $this->whereIn($this->pk, $ids)->select();
        foreach ($data as $val) {
            $return[$val[$this->pk]] = $val;
        }
        return $return;
    }

    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", request()->time());
        $model->set("add_ip", request()->ip());
    }
     public function getAddTimeFormatAttr($value,$data)
    {
       
        return $data['add_time'] > 0 ? date('Y-m-d H:i:s',$data['add_time']) : '-';
    }
     public function getSmallAddTimeFormatAttr($value,$data)
     {
        return $data['add_time'] > 0 ? date('Y-m-d',$data['add_time']) : '-';
     }
    
    public function getLastTimeFormatAttr($value,$data){
        
        return $data['last_time'] > 0 ? date('Y-m-d H:i:s',$data['last_time']) : '-';
    }
     public function n_inc($where,$field,$num){
         $table=$this->name;
          return Db::name($table)
             ->where($where)
             ->inc($field,$num)
             ->update();
     }

     public function n_dec($where,$field,$num){
         $table=$this->name;
         return Db::name($table)
             ->where($where)
             ->dec($field,$num)
             ->update();
     }
}
